package ro.myApp.esb.service;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import ro.myApp.commons.dto.BaseDtoMessage;
import ro.myApp.esb.messages.EsbMessage;
import ro.myApp.esb.messages.EsbMessageTransactionNew;
import ro.myApp.esb.messages.EsbMessageTransactionNotSupported;
import ro.myApp.esb.messages.EsbMessageTransactionRequired;

public abstract class MessageBasedServiceImpl implements MessageBasedService{

	@Override
	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public EsbMessage receiveMessageSynchTransactionNotSupported(
			EsbMessageTransactionNotSupported message) throws Throwable {
		return receiveMessage(message);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public EsbMessage receiveMessageSynchTransactionRequired(
			EsbMessageTransactionRequired message) throws Throwable {
		return receiveMessage(message);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public EsbMessage receiveMessageSynchTransactionNew(EsbMessageTransactionNew message)
			throws Throwable {
		return receiveMessage(message);
	}
	
	private EsbMessage receiveMessage (EsbMessage message) throws Throwable{
		message.setReplyMessage(execute(message.getWrappedMessage()));
		return message;
	}
	
	protected abstract Object execute (BaseDtoMessage dto) throws Throwable;

}
